\section{Source/Surface.c}


\begin{footnotesize}\begin{alltt}
00001 \textcolor{preprocessor}{#include <math.h>}
00002 \textcolor{preprocessor}{#include "Math.h"}
00003 \textcolor{preprocessor}{#include "Surface.h"}
00004 
00005 \textcolor{keywordtype}{void} Surface\_DrawPixel(SDL\_Surface *surface, \textcolor{keywordtype}{int} x, \textcolor{keywordtype}{int} y, Uint32 pixel)
00006 \{
00007     \textcolor{keywordtype}{int} bpp = surface->format->BytesPerPixel;
00008     \textcolor{comment}{/* Here p is the address to the pixel we want to set */}
00009     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00010 
00011     \textcolor{keywordflow}{switch}(bpp) \{
00012     \textcolor{keywordflow}{case} 1:
00013         *p = pixel;
00014         \textcolor{keywordflow}{break};
00015 
00016     \textcolor{keywordflow}{case} 2:
00017         *(Uint16 *)p = pixel;
00018         \textcolor{keywordflow}{break};
00019 
00020     \textcolor{keywordflow}{case} 3:
00021         \textcolor{keywordflow}{if}(SDL\_BYTEORDER == SDL\_BIG\_ENDIAN) \{
00022             p[0] = (pixel >> 16) & 0xff;
00023             p[1] = (pixel >> 8) & 0xff;
00024             p[2] = pixel & 0xff;
00025         \} \textcolor{keywordflow}{else} \{
00026             p[0] = pixel & 0xff;
00027             p[1] = (pixel >> 8) & 0xff;
00028             p[2] = (pixel >> 16) & 0xff;
00029         \}
00030         \textcolor{keywordflow}{break};
00031 
00032     \textcolor{keywordflow}{case} 4:
00033         *(Uint32 *)p = pixel;
00034         \textcolor{keywordflow}{break};
00035     \}
00036 \}
00037 
00038 Uint32 Surface\_ReadPixel(SDL\_Surface *surface, \textcolor{keywordtype}{int} x, \textcolor{keywordtype}{int} y)
00039 \{
00040     \textcolor{keywordtype}{int} bpp = surface->format->BytesPerPixel;
00041     \textcolor{comment}{/* Here p is the address to the pixel we want to retrieve */}
00042     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00043 
00044     \textcolor{keywordflow}{switch}(bpp) \{
00045     \textcolor{keywordflow}{case} 1:
00046         \textcolor{keywordflow}{return} *p;
00047         \textcolor{keywordflow}{break};
00048 
00049     \textcolor{keywordflow}{case} 2:
00050         \textcolor{keywordflow}{return} *(Uint16 *)p;
00051         \textcolor{keywordflow}{break};
00052 
00053     \textcolor{keywordflow}{case} 3:
00054         \textcolor{keywordflow}{if}(SDL\_BYTEORDER == SDL\_BIG\_ENDIAN)
00055             \textcolor{keywordflow}{return} p[0] << 16 | p[1] << 8 | p[2];
00056         \textcolor{keywordflow}{else}
00057             \textcolor{keywordflow}{return} p[0] | p[1] << 8 | p[2] << 16;
00058         \textcolor{keywordflow}{break};
00059 
00060     \textcolor{keywordflow}{case} 4:
00061         \textcolor{keywordflow}{return} *(Uint32 *)p;
00062         \textcolor{keywordflow}{break};
00063 
00064     \textcolor{keywordflow}{default}:
00065         \textcolor{keywordflow}{return} 0;       \textcolor{comment}{/* shouldn't happen, but avoids warnings */}
00066     \}
00067 \}
00068 
00069 SDL\_Surface *Surface\_ScaleSurfaceFast(SDL\_Surface *Surface, Uint16 Width, Uint16 
      Height)
00070 \{   
00071     SDL\_Surface *Return = SDL\_CreateRGBSurface(Surface->flags, Width, Height, Sur
      face->format->BitsPerPixel,
00072         Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, S
      urface->format->Amask);
00073 
00074     \textcolor{keywordtype}{int} X, Y;
00075     \textcolor{keywordtype}{float} OldX, OldY, XInc=Surface->w/Width, YInc=Surface->h/Height;
00076     
00077     OldY=0;
00078     \textcolor{keywordflow}{for}(Y=0;Y<Height;++Y)
00079     \{
00080         OldX=0;
00081         \textcolor{keywordflow}{for}(X=0;X<Width;++X)
00082         \{
00083             Surface\_DrawPixel(Return, (Sint32)X, (Sint32)Y, Surface\_ReadPixel(Sur
      face, (Sint32)OldX, (Sint32)OldY));
00084             OldX+=XInc;
00085         \}
00086         OldY+=YInc;
00087     \}
00088 
00089     \textcolor{keywordflow}{return} Return;
00090 \}
00091 
00092 SDL\_Surface *Surface\_RotateSurface(SDL\_Surface *Surface, \textcolor{keywordtype}{float} RotationAngle, \textcolor{keywordtype}{int
      } Crop)
00093 \{
00094     SDL\_Surface *\_ret = SDL\_CreateRGBSurface(Surface->flags, Surface->w, Surface-
      >h, Surface->format->BitsPerPixel,
00095         Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, S
      urface->format->Amask);
00096 
00097     \textcolor{keywordtype}{int} X, Y, CentreX=Surface->w/2, CentreY=Surface->h/2, OldX, OldY;
00098     \textcolor{keywordtype}{float} DistanceToCentre, AngleFromCentre, NewAngle;
00099     \textcolor{keywordflow}{for}(Y=0;Y<Surface->h;++Y)
00100     \{
00101         \textcolor{keywordflow}{for}(X=0;X<Surface->w;++X)
00102         \{
00103             DistanceToCentre=sqrt((X-CentreX)*(X-CentreX)+(Y-CentreY)*(Y-CentreY)
      );
00104             
00105             \textcolor{keywordflow}{if} (!Crop || DistanceToCentre<Surface->w/2 || DistanceToCentre<Surfac
      e->h/2)
00106             \{
00107                 AngleFromCentre=Math\_XYAngle(CentreX, CentreY, X, Y);
00108                 NewAngle=Math\_NormaliseAngle(AngleFromCentre-RotationAngle);
00109                 
00110                 OldX=((int)(CentreX+Math\_AngleX(NewAngle)*DistanceToCentre));
00111                 OldY=((int)(CentreY+Math\_AngleY(NewAngle)*DistanceToCentre));
00112                 
00113                 \textcolor{keywordflow}{if} (OldX>=0 && OldX<Surface->w && OldY>=0 && OldY<Surface->h)
00114                     Surface\_DrawPixel(\_ret, X, Y, Surface\_ReadPixel(Surface, OldX
      , OldY));
00115             \}
00116         \}
00117     \}
00118     
00119     \textcolor{keywordflow}{return} \_ret;
00120 \}
\end{alltt}\end{footnotesize}
